package io.kiki.sba.registry.server.data_store.bootstrap;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.SmartLifecycle;


public class DataServerInitializer implements SmartLifecycle {
    private static final Logger logger = LoggerFactory.getLogger(DataServerInitializer.class);

    @Autowired
    private Bootstrap bootstrap;

    private volatile boolean isRunning;

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable runnable) {
        runnable.run();
        this.isRunning = false;
    }

    @Override
    public void start() {
        try {
            bootstrap.start();
            this.isRunning = true;
        } catch (Throwable ex) {
            this.isRunning = false;
            logger.error("Could not initalized Data server", ex);
            Runtime.getRuntime().halt(-1);
        }
    }

    @Override
    public void stop() {
        this.isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return this.isRunning;
    }

    @Override
    public int getPhase() {
        return 0;
    }
}
